#!/bin/sh

# PROMPT
case ${TERM} in
  xterm*|rxvt*|Eterm|aterm|kterm|gnome*)
  PROMPT_COMMAND='echo -ne "\033]0;${PWD/$HOME/~}\007"'
  ;;
  screen)
  PROMPT_COMMAND='echo -ne "\033_${PWD/$HOME/~}\033\\"'
  ;;
esac

# set a fancy prompt (non-color, unless we know we "want" color)

function parse_scm {
  if [ -d '.git' ];then
    GIT_BRANCH=`git branch 2> /dev/null | grep '^\*' | sed 's/^\*\ //'`
    if [ "$GIT_BRANCH" ] ; then
      GIT_STATUS=`git status 2> /dev/null`
      CLR='1'
      if [ "`echo $GIT_STATUS | grep 'nothing to commit'`" ] ; then
        CLR='2'
      fi

      pt=`echo $GIT_STATUS | grep 'Your branch is ahead' | awk {'print $13'}`
      if [ "${pt}" ] ; then
        PTIMES=" ${pt} ↑ "
      else
        PTIMES=""
      fi

      RSLT="     \e[01;3${CLR}m[${GIT_BRANCH}${PTIMES}]"
    fi
    echo -e "${RSLT}" && return
  fi

  if [ -d '.svn' ];then
    local DIRTY REV=$(svn info 2>/dev/null | grep Revision | sed -e 's/Revision: //')
    [ "$REV" ] || return
    
    CLR='2'
    if [ "$(svn st)" ] ; then
      CLR='1'
    fi
    
    echo -e "     \e[01;3${CLR}m[r$REV$DIRTY]" && return
  fi
}

export PS1='\[\e[01;30m\] [\u@\h]     \[\e[01;36m\][\t]     \[\e[01;34m\][\W]$(parse_scm)\[\e[0m
\[\e[01;32m\]\$\[\e[0m\]$(pwd > /tmp/.last_dir) '
